package com.oldroid.apps.getit.util;

import java.io.File;
import java.io.FilePermission;
import java.util.ArrayList;

import com.dropbox.client.DropboxAPI;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;


// parameters progress result
public class UploadAsync extends AsyncTask<DropBox, String, Integer> {

	private Context  mActitivty;
	 
	
	
	private String filePath;
	private String uploadDir;
	private boolean delete;
	
	public String[] StringParams; 
	
	/**
	 * 
	 * @param activity - the activity where the upload op will run
	 * @param filePath - the Absolute File Path of the file to be upload
	 * @param uploadDir - the folder on dropbox to send the file. It should start with "/". 
	 * For example "/public"
	 * @param del  indicates whether the file is to be deleted after uploading to dropbox. 
	 */
	public UploadAsync (Context activity, String filePath, String uploadDir, boolean del){
		
		this.filePath = filePath;
		this.uploadDir = uploadDir;
		this.delete = del;
		
		mActitivty = activity;
		
		
		
	}
	
	// Runs on main thread.
    @Override
    protected void onProgressUpdate(String... msgs) {
        if (msgs[0] != null) {
            showToast(msgs[0]);
        }
    }
	
	//
	protected Integer doInBackground(DropBox... params) {
		
		DropBox db = params[0];
		
		publishProgress("Uploading to DropBox");
		 
		// Print log msg
		log("upload path is " + uploadDir + " and filepath=" + filePath); 
		
		File file = new File(filePath);
		
		if(!file.exists()){ // if file not found
			publishProgress("Uploaded stopped - File was not Found. Please Make "  +
							"Sure File exists and try again");
			return null; 
		}
		
		db.uploadFile(file, uploadDir);
		
		publishProgress("Uploading complete");
		if(delete)
			file.delete();
		log("File Upload complete");
		return DropboxAPI.STATUS_SUCCESS; // succeeded
    }
	
	
	@Override
	protected void onPostExecute(Integer result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		
		
		log("UPLOAD FINISHED");
		//showToast("Upload completed!");
	
		return ; 
	}

	
	
	
	public void log(String msg){
		
		Log.d("DROPBOX", msg);
	}
	
	
	public void showToast(String msg) {
		Toast error = Toast.makeText(mActitivty, msg, Toast.LENGTH_LONG);
        error.show();
    }
	
	
}
